package com.yaoxun.bored.shiro.credential;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;

import com.yaoxun.bored.shiro.credential.interceptor.MatcherInterceptor;

public class PasswordLogicCredentialsMatcher extends HashedCredentialsMatcher {
	
	private List<MatcherInterceptor> interceptors;
	
	@Override
	public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
		if(false == CollectionUtils.isEmpty(interceptors)) {
			for(MatcherInterceptor interceptor : interceptors) {
				if(false == interceptor.preHandle(token, info)) {
					return false;
				}
			}
			
			boolean match = super.doCredentialsMatch(token, info);
			
			if(match) {
				for(MatcherInterceptor interceptor : interceptors) {
					interceptor.afterHandle(token, info);
				}
			}
			return match;
		}else {			
			return super.doCredentialsMatch(token, info);
		}
	}
	
}
